using AnalyStock.DataManage;

namespace AnalyStock.GrapImage;

/// <summary>
///     Kline数据查询日期改变，鼠标移动或滚动变化K线图
/// </summary>
public partial class ChartKline : UserControl
{
    private enum DaysChangeType
    {
        IniDays, AddDays, SubDays, LeftMove, RightMove, NoChange
    }
    private string startDate = DateTime.Today.AddDays(-120).ToDateString();
    private string endDate = DateTime.Today.ToDateString();
    private bool isChangeKline;
    private bool isLockQueryDate;
    private Point lastPosition;

    private void InitializeKlineMouseEvent()
    {
        ChartCandle.MouseWheel += ChangQueryDate_MouseWheel;
        ChartCandle.MouseLeftButtonDown += ChartChandle_MouseLeftButtonDown;
        ChartCandle.MouseLeftButtonUp += ChartChandle_MouseLeftButtonUp;
        ChartCandle.MouseMove += ChartChandle_MouseMove;
        ChangeKlineDatesPanel.InitializeMouseDarg(this);
    }

    private void ChangQueryDate_Click(object sender, RoutedEventArgs e)
    {
        var _typeChange = (sender as Button).Name switch
        {
            nameof(BtnAddDates) => DaysChangeType.AddDays,
            nameof(BtnSubDates) => DaysChangeType.SubDays,
            _ => DaysChangeType.IniDays
        };
        ChangeKlineData(_typeChange);
    }

    private void LockQueryDates_Click(object sender, RoutedEventArgs e)
    {
        isLockQueryDate = !isLockQueryDate;
        var _btnLock = sender as Button;
        if (isLockQueryDate)
        {
            _btnLock.Content = "🔒";
            _btnLock.Background = Brushes.DarkSlateGray;
            _btnLock.Background = FindResource("AreaBackColorWarm") as SolidColorBrush;
        }
        else
        {
            _btnLock.Content = "🔓";
            _btnLock.Background = Brushes.DarkGray;
            _btnLock.Background = FindResource("AreaBackColorDark") as SolidColorBrush;
        }
    }

    private void ChangQueryDate_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        ChangeKlineData(e.Delta < 0 ? DaysChangeType.AddDays : DaysChangeType.SubDays);
        e.Handled = true;
    }

    //鼠标移动 
    private void ChartChandle_MouseMove(object sender, MouseEventArgs e)
    {
        if (isChangeKline)
        {
            //var currEle = sender as FrameworkElement;
            var offset = e.GetPosition(null).X - lastPosition.X;
            lastPosition = e.GetPosition(null);
            if (offset == 0)
            {
                return;
            }

            ChangeKlineData(offset < 0 ? DaysChangeType.LeftMove : DaysChangeType.RightMove);
            e.Handled = true;
        }
    }

    //鼠标松开
    private void ChartChandle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (isChangeKline)
        {
            isChangeKline = false;
            //释放鼠标捕获
            (sender as FrameworkElement).ReleaseMouseCapture();
            (sender as FrameworkElement).Cursor = Cursors.Arrow;
            e.Handled = true;
        }
        // 
    }

    //鼠标按下
    private void ChartChandle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        isChangeKline = true;
        lastPosition = e.GetPosition(null);
        //创建鼠标捕获
        (sender as FrameworkElement).CaptureMouse();
        (sender as FrameworkElement).Cursor = Cursors.Hand;
        e.Handled = true;
    }

    private void ChangeQueryDates(DaysChangeType type)
    {
        var _startdate = startDate.ConvertToDate();
        var _enddate = endDate.ConvertToDate();
        var (days_IniDate, days_ClickChange, days_MouseMove) = currPeriodType switch
        {
            CandlePointSet.PeriodType.Daily => (120, 60, 5),
            CandlePointSet.PeriodType.Weekly => (560, 120, 28),
            CandlePointSet.PeriodType.Monthly => (900, 240, 60),
            _ => (0, 0, 0)
        };
        switch (type)
        {
            case DaysChangeType.IniDays: //恢复到初始期间            
                _startdate = DateTime.Today.AddDays(-days_IniDate); //显示半年内的数据   
                _enddate = DateTime.Today;
                break;
            case DaysChangeType.AddDays: //缩小k线，增加面板蜡烛图数据
                if (_startdate > DateTime.Today.AddYears(-5))
                {
                    _startdate = _startdate.AddDays(-days_ClickChange);
                }
                break;
            case DaysChangeType.SubDays: //放大K线，减少面板蜡烛图数据
                if (_startdate.AddDays(days_ClickChange) < DateTime.Today)
                {
                    if (ChartCandle.totalIndex > 70)
                    {
                        _startdate = _startdate.AddDays(days_ClickChange);
                    }
                }
                break;
            case DaysChangeType.LeftMove: //向左平移
                if (_enddate <= DateTime.Today.AddDays(-days_MouseMove))
                {
                    _startdate = _startdate.AddDays(days_MouseMove);
                    _enddate = _enddate.AddDays(days_MouseMove);
                }
                break;
            case DaysChangeType.RightMove: //向右平移
                if (_startdate > DateTime.Today.AddYears(-5)) //往后移动3年的数据，否则不移动了
                {
                    if (ChartCandle.totalIndex < 70)
                    {
                        _startdate = _startdate.AddDays(-days_MouseMove);
                        _enddate = DateTime.Today;
                    }
                    else
                    {
                        _startdate = _startdate.AddDays(-days_MouseMove);
                        _enddate = _enddate.AddDays(-days_MouseMove);
                    }
                }
                break;
        }
        startDate = _startdate.ToDateString();
        endDate = _enddate.ToDateString();
    }

}